<?php

declare(strict_types=1);

namespace App\Model;

use App\Constants\OrderPayStatus;
use App\Constants\OrderStatus;
use App\Overwrites\MineModel;
use Carbon\Carbon;
use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\HasOne;

/**
 * @property int $id
 * @property int $member_id 用户ID
 * @property int $car_id 车辆ID
 * @property int $channel_id 渠道ID
 * @property int $staff_id 员工ID
 * @property int $no 订单号
 * @property string $city 城市
 * @property string $address 地址
 * @property Carbon $start_at 开始时间
 * @property Carbon $end_at 结束时间
 * @property int $day 租车天数
 * @property int $price 租车单价
 * @property int $cleaning_fee 清洁费
 * @property int $service_fee 服务费
 * @property int $insurance_price 保险单价
 * @property int $total_price 总价格
 * @property string $pay_no 支付单号
 * @property Carbon $pay_launch_at 支付发起时间
 * @property Carbon $pay_confirm_at 支付确认时间
 * @property Carbon $pay_lapse_at 支付失效时间
 * @property int $pay_status 支付状态:0=未支付,1=支付中,2=付款成功
 * @property int $is_delivery 是否送达:0=否,1=是
 * @property Carbon $delivery_at 送达时间
 * @property Carbon $cancelled_at 取消时间
 * @property Carbon $return_at 还车时间
 * @property int $status 状态:0=待支付,1=进行中,2=已完成,3=已取消,4=已失效
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property-read Member|null $member
 * @property-read Staff|null $staff
 * @property-read OrderCar|null $orderCar
 * @property-read OrderLessee|null $orderLessee
 * @property-read OrderInsurance|null $orderInsurance
 * @property-read string $pay_status_text
 * @property-read string $is_delivery_text_text
 * @property-read string $status_text
 */
class Order extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'orders';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'member_id', 'car_id', 'channel_id', 'staff_id', 'no', 'city', 'address', 'start_at', 'end_at', 'day', 'price', 'cleaning_fee', 'service_fee', 'insurance_price', 'total_price', 'pay_no', 'pay_launch_at', 'pay_confirm_at', 'pay_lapse_at', 'pay_status', 'is_delivery', 'delivery_at', 'cancelled_at', 'return_at', 'status', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'member_id' => 'integer', 'car_id' => 'integer', 'channel_id' => 'integer', 'staff_id' => 'integer', 'start_at' => 'datetime', 'end_at' => 'datetime', 'no' => 'integer', 'day' => 'integer', 'price' => 'integer', 'cleaning_fee' => 'integer', 'service_fee' => 'integer', 'insurance_price' => 'integer', 'total_price' => 'integer', 'pay_launch_at' => 'datetime', 'pay_confirm_at' => 'datetime', 'pay_lapse_at' => 'datetime', 'pay_status' => 'integer', 'is_delivery' => 'integer', 'delivery_at' => 'datetime', 'cancelled_at' => 'datetime', 'return_at' => 'datetime', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    protected function getPayStatusTextAttribute(): string
    {
        return OrderPayStatus::getMessage($this->pay_status);
    }

    protected function getIsDeliveryTextAttribute(): string
    {
        return $this->is_delivery ? '是' : '否';
    }

    protected function getStatusTextAttribute(): string
    {
        return OrderStatus::getMessage($this->status);
    }

    public function member(): BelongsTo
    {
        return $this->belongsTo(Member::class);
    }

    public function staff(): BelongsTo
    {
        return $this->belongsTo(Staff::class);
    }

    public function orderCar(): HasOne
    {
        return $this->hasOne(OrderCar::class, 'order_id');
    }

    public function orderLessee(): HasOne
    {
        return $this->hasOne(OrderLessee::class, 'order_id');
    }

    public function orderInsurance(): HasOne
    {
        return $this->hasOne(OrderInsurance::class, 'order_id');
    }
}
